package com.lin.controller;


import cn.hutool.crypto.SecureUtil;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lin.commons.Result;
import com.lin.dto.UserDTO;
import com.lin.entity.User;
import com.lin.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 用户业务前端控制器
 * </p>
 *
 * @author 林伟鸿
 * @since 2021-11-24
 */
@RestController
@RequestMapping("/users")
@CrossOrigin
@Slf4j
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 用户登陆接口
     * @param userDTO 登陆实体类
     * @param session Session记录用户
     * @return 统一结果集Result
     */
    @PostMapping(value = "/login")
    public Result login(@RequestBody UserDTO userDTO, HttpSession session) {
        //调用service层登陆，并且托管给Shiro进行验证
        int login = userService.login(userDTO);
        if (login == 1) {
            //如果登陆成功，记录进Session
            User user = (User) session.getAttribute("user");
            //将用户实体返回
            return Result.success(user);
            //异常信息进行封装返回
        } else if (login == 2) {
            return Result.failure("用户名错误");
        } else {
            return Result.failure("密码错误");
        }
    }

    /**
     * 用户注册接口
     * @param user 获得一个用户实体
     * @return 统一结果封装Result
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        int register = userService.register(user);
        if (register == 1) {
            return Result.success();
        } else {
            return Result.failure();
        }
    }

    /**
     * 验证用户旧密码接口
     * @param oldPassword 旧密码
     * @param userId 用户编号
     * @return 统一结果集Result
     */
    @PostMapping("/verifyPwd")
    public Result modifyPwd(@RequestParam("oldPassword") String oldPassword, String userId) {
        User user = userService.getById(Integer.parseInt(userId));
        if (user.getPassword().equals(SecureUtil.md5(oldPassword))) {
            return Result.success();
        } else {
            return Result.failure();
        }
    }

    /**
     * 用户修改密码结构
     * @param newPassword 新密码
     * @param userId 用户编号
     * @return 统一结果集Result
     */
    @PostMapping("/updateNewPwd")
    public Result updateNewPwd(@RequestParam("newPassword") String newPassword, String userId) {
        //调用service层
        int i = userService.updatePwd(newPassword, Integer.parseInt(userId));
        //逻辑判断
        if (i == 1) {
            //结果返回
            return Result.success();
        } else {
            return Result.failure();
        }
    }

    /**
     * 用户注销功能
     * @return 统一结果集Result
     */
    @GetMapping("/logout")
    public Result logout() {
        //托管给shiro进行注销，会清除session
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return Result.success("退出成功");
    }



}

